package com.mashibing;

import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;

import java.io.IOException;
import java.util.concurrent.CountDownLatch;

/**
 * Hello world!
 */
public class App {
    public static void main(String[] args) throws IOException, InterruptedException, KeeperException {
        System.out.println("Hello World!");


        final CountDownLatch countDownLatch = new CountDownLatch(1);

        //watcher观察、回调
        //watch的注册只发生在读数据时 如：get exist
        //第一类：这个watch是session级别的，跟node、path没关系
        final ZooKeeper zk = new ZooKeeper("192.168.1.10:2181,192.168.1.20:2181,192.168.1.30:2181", 3000, new Watcher() {
            //watch的回调方法
            @Override
            public void process(WatchedEvent event) {
                Event.KeeperState state = event.getState();
                Event.EventType type = event.getType();
                String path = event.getPath();
                System.out.println("new zk watch: " + event.toString());

                switch (state) {
                    case Unknown:
                        break;
                    case Disconnected:
                        break;
                    case NoSyncConnected:
                        break;
                    case SyncConnected:
                        System.out.println("connected");
                        countDownLatch.countDown();
                        break;
                    case AuthFailed:
                        break;
                    case ConnectedReadOnly:
                        break;
                    case SaslAuthenticated:
                        break;
                    case Expired:
                        break;
                    case Closed:
                        break;
                }

                switch (type) {
                    case None:
                        break;
                    case NodeCreated:
                        System.out.println("node create");
                        break;
                    case NodeDeleted:
                        break;
                    case NodeDataChanged:
                        break;
                    case NodeChildrenChanged:
                        break;
                    case DataWatchRemoved:
                        break;
                    case ChildWatchRemoved:
                        break;
                }
            }
        });

        countDownLatch.await();
        ZooKeeper.States state = zk.getState();
        switch (state) {
            case CONNECTING:
                System.out.println("zk connecting...");
                break;
            case ASSOCIATING:
                break;
            case CONNECTED:
                System.out.println("zk connected...");
                break;
            case CONNECTEDREADONLY:
                break;
            case CLOSED:
                break;
            case AUTH_FAILED:
                break;
            case NOT_CONNECTED:
                break;
        }

        String pathName = zk.create("/duomi", "oldData".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);

        final Stat stat = new Stat();
        byte[] node = zk.getData("/duomi", new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                System.out.println("getData watch :" + event.toString());
                try {
                    //当watch为true时，传递的是default wathc,是new zk的那个watch
                    zk.getData("/duomi",this,stat);
                } catch (KeeperException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, stat);

        System.out.println(new String(node));

        //触发回调
        Stat stat1 = zk.setData("/duomi", "newData".getBytes(), 0);
        //再次修改node信息会触发吗？
        Stat stat2 = zk.setData("/duomi", "newData01".getBytes(), stat1.getVersion());

        //异步获取数据
        System.out.println("async start");
        zk.getData("/duomi", false, new AsyncCallback.DataCallback() {
            @Override
            public void processResult(int rc, String path, Object ctx, byte[] data, Stat stat) {
                System.out.println("async call back");
                System.out.println(ctx);
                System.out.println(new String(data));
            }
        },"abc");
        System.out.println("async over");


        Thread.sleep(22222222);
    }
}
